package org.jeecg.modules.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.modules.common.entity.SwgxJbKhxxDo;
import org.jeecg.modules.common.mapper.SwgxJbKhxxMapper;
import org.jeecg.modules.common.service.SwgxJbKhxxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class SwgxJbKhxxServiceImpl extends ServiceImpl<SwgxJbKhxxMapper, SwgxJbKhxxDo> implements SwgxJbKhxxService {

    @Autowired
    private SwgxJbKhxxMapper swgxJbKhxxMapper;
    @Override
    public void dzdhClean() {
        QueryWrapper<SwgxJbKhxxDo> khxxDoQueryWrapper = new QueryWrapper<>();
        khxxDoQueryWrapper.ne("dzdh","");
        List<SwgxJbKhxxDo> khxxlist = swgxJbKhxxMapper.selectList(khxxDoQueryWrapper);
        int limit = 1000;
        int max = khxxlist.size();
        int maxSize = max - 1;
        List<SwgxJbKhxxDo> newlist = new ArrayList<>();
        for (int i = 0; i < max; i++) {
            newlist.add(khxxlist.get(i));
            if (limit == newlist.size() || i == maxSize) {  //载体list达到要求,进行批量操作
                for (int j = 0; j < newlist.size(); j++) {
                    boolean update = false;
                    SwgxJbKhxxDo swgxJbKhxxDo = newlist.get(j);
                    //处理客户地址和客户电话  如果已经分开 则不再处理
                    if (StringUtils.isNotBlank(swgxJbKhxxDo.getKhdz()) && StringUtils.isNotBlank(swgxJbKhxxDo.getKhdh())
                            && StringUtils.isNotBlank(swgxJbKhxxDo.getYhmc()) && StringUtils.isNotBlank(swgxJbKhxxDo.getYhzh())) {
                        continue;
                    }
                    String dzdh = swgxJbKhxxDo.getDzdh();
                    //首先处理有空格的情况
                    if (StringUtils.isNotBlank(dzdh) && dzdh.contains(" ")) {
                        String[] dzdharray = dzdh.split(" ");
                        if(dzdharray.length == 2){
                            String dz = dzdharray[0];
                            String dh = dzdharray[1];
                            if (StringUtils.isNotBlank(dz) && StringUtils.isBlank(swgxJbKhxxDo.getKhdz())) {
                                update = true;
                                swgxJbKhxxDo.setKhdz(dz);
                            }
                            if (StringUtils.isNotBlank(dh) && StringUtils.isBlank(swgxJbKhxxDo.getKhdh())) {
                                update = true;
                                swgxJbKhxxDo.setKhdh(dh);
                            }
                        }
                    } else {
                        //处理其他情况 用正则表达式
                        /**
                         * 1  010-1234567  \d{3}-\d{7}
                         * 2  010-12345678  \d{3}-\d{8}
                         * 3  0371-1234567   \d{4}-\d{7}
                         * 4  0371-12345678  \d{4}-\d{8}
                         * 5  0101234567    \d{10}
                         * 6  01012345678   \d{11}
                         * 7  03711234567    \d{11}
                         * 8  037112345678   \d{12}
                         * 9  13838000000   \d{11}
                         */
                        String pattern = "^(((\\(\\d{2,3}\\))|(\\d{3}\\-))?13\\d{9})|(((\\(\\d{2,3}\\))|(\\d{3}\\-))?(\\(0\\d{2,3}\\)|0\\d{2,3}-)?[1-9]\\d{6,7}(\\-\\d{1,4})?)$|1[3456789]\\d{9}|^(((\\(\\d{2,3}\\))|(\\d{3}))?13\\d{9})|(((\\(\\d{2,3}\\))|(\\d{3}))?(\\(0\\d{2,3}\\)|0\\d{2,3}-)?[1-9]\\d{6,7}(\\d{1,4})?)$";
                        Pattern regex = Pattern.compile(pattern);
                        Matcher matcher = regex.matcher(dzdh);
                        while (matcher.find()) {
                            String dh = matcher.group();
                            String dz = dzdh.replace(dh, "");
                            if (StringUtils.isNotBlank(dz) && StringUtils.isBlank(swgxJbKhxxDo.getKhdz())) {
                                update = true;
                                swgxJbKhxxDo.setKhdz(dz);
                            }
                            if (StringUtils.isNotBlank(dh) && StringUtils.isBlank(swgxJbKhxxDo.getKhdh())) {
                                update = true;
                                swgxJbKhxxDo.setKhdh(dh);
                            }
                        }
                    }

                    //其他情况 如果依然没有赋值成功  那就用dzdh 放到 khdz当中、
                    if(StringUtils.isBlank(swgxJbKhxxDo.getKhdz())){
                        update = true;
                        swgxJbKhxxDo.setKhdz(swgxJbKhxxDo.getDzdh());
                    }
                    if(update){
                        swgxJbKhxxMapper.updateById(swgxJbKhxxDo);
                    }
                }
                newlist.clear();//每次批量操作后,清空载体list,等待下次的数据填入
            }
        }
    }

    @Override
    public void yhzhClean() {
        QueryWrapper<SwgxJbKhxxDo> yhzhDoQueryWrapper = new QueryWrapper<>();
        yhzhDoQueryWrapper.ne("yhzh","");
        List<SwgxJbKhxxDo> yhzhlist = swgxJbKhxxMapper.selectList(yhzhDoQueryWrapper);
        int limit = 1000;
        int max = yhzhlist.size();
        int maxSize = max - 1;
        List<SwgxJbKhxxDo> newlist = new ArrayList<>();
        for (int i = 0; i < max; i++) {
            newlist.add(yhzhlist.get(i));
            if (limit == newlist.size() || i == maxSize) {  //载体list达到要求,进行批量操作
                for (int j = 0; j < newlist.size(); j++) {
                    boolean update = false;
                    SwgxJbKhxxDo swgxJbKhxxDo = newlist.get(j);
                    //处理客户地址和客户电话  如果已经分开 则不再处理
                    if (StringUtils.isNotBlank(swgxJbKhxxDo.getKhdz()) && StringUtils.isNotBlank(swgxJbKhxxDo.getKhdh())
                            && StringUtils.isNotBlank(swgxJbKhxxDo.getYhmc()) && StringUtils.isNotBlank(swgxJbKhxxDo.getYhzh())) {
                        continue;
                    }
                    String yhzh = swgxJbKhxxDo.getYhzh();
                    //首先处理有空格的情况
                    if (StringUtils.isNotBlank(yhzh) && yhzh.contains(" ")) {
                        String[] yhzharray = yhzh.split(" ");
                        if(yhzharray.length == 2){
                            String yh = yhzharray[0];
                            String zh = yhzharray[1];
                            if (StringUtils.isNotBlank(yh) && StringUtils.isBlank(swgxJbKhxxDo.getYhmc())) {
                                update = true;
                                swgxJbKhxxDo.setYhmc(yh);
                            }
                            if (StringUtils.isNotBlank(zh) && StringUtils.isBlank(swgxJbKhxxDo.getYhkh())) {
                                update = true;
                                swgxJbKhxxDo.setYhkh(zh);
                            }
                        }
                    }
                    //其他情况 如果依然没有赋值成功  那就用dzdh 放到 khdz当中、
                    if(StringUtils.isBlank(swgxJbKhxxDo.getYhmc())){
                        update = true;
                        swgxJbKhxxDo.setYhmc(swgxJbKhxxDo.getYhzh());
                    }

                    if(update){
                        swgxJbKhxxMapper.updateById(swgxJbKhxxDo);
                    }
                }
                newlist.clear();//每次批量操作后,清空载体list,等待下次的数据填入
            }
        }
    }


    public static void main(String[] args) {
        String pattern1 = "^(((\\(\\d{2,3}\\))|(\\d{3}\\-))?13\\d{9})|(((\\(\\d{2,3}\\))|(\\d{3}\\-))?(\\(0\\d{2,3}\\)|0\\d{2,3}-)?[1-9]\\d{6,7}(\\-\\d{1,4})?)$|1[3456789]\\d{9}|^(((\\(\\d{2,3}\\))|(\\d{3}))?13\\d{9})|(((\\(\\d{2,3}\\))|(\\d{3}))?(\\(0\\d{2,3}\\)|0\\d{2,3}-)?[1-9]\\d{6,7}(\\d{1,4})?)$";
        String pattern2 = "^(((\\(\\d{2,3}\\))|(\\d{3}))?13\\d{9})|(((\\(\\d{2,3}\\))|(\\d{3}))?(\\(0\\d{2,3}\\)|0\\d{2,3}-)?[1-9]\\d{6,7}(\\d{1,4})?)$";
        String pattern3 = "1[3456789]\\d{9}";
        String taget = "郑州高新技术产业开发区科学大道81号地质调查院院内4-3-3西13663015810";
        String pattern = "\\d{4}-\\d{7}";
        Pattern regex = Pattern.compile(pattern1);
        Matcher matcher = regex.matcher(taget);
        while (matcher.find()) {
            String phoneNumber = matcher.group();
            System.out.println("提取到的电话号码：" + phoneNumber);
            System.out.println("剩余地址信息：" + taget.replace(phoneNumber, ""));
        }
        System.out.println("未找到");
    }
}
